package com.eimf.saf.base.service;

import java.util.HashMap;
import java.util.List;

import com.eimf.saf.base.dao.BaseDao;
import com.eimf.saf.base.querycomm.SearchExpression;
import com.eimf.saf.base.util.Page;

/**
 * @author eden
 * @param <E>
 * @date 2013-11-18 下午2:11:15
 * @Description: TODO(这里用一句话描述这个类的作用)
 */
public abstract class AbstractBaseService<E>
{
    public abstract BaseDao<E> getBaseDao();

    public String addData(final E entity)
    {
        return getBaseDao().addData(entity);
    }

    public void updateData(final E entity)
    {
        getBaseDao().updateData(entity);
    }

    public void delData(final E entity)
    {
        getBaseDao().delData(entity);
    }

    public E find(final SearchExpression searchExp)
    {
        return getBaseDao().find(searchExp);
    }

    public List<E> findList(final SearchExpression searchExp)
    {
        return getBaseDao().findList(searchExp);
    }

    public Page findListToPage(final SearchExpression searchExp, final Page page)
    {
        final HashMap<String, Object> param = new HashMap<String, Object>();

        // 先设置总行数以及查询参数
        page.setTotalRows(param, getBaseDao().findTotalRows(searchExp));

        searchExp.setStartRow(page.getStartRow());
        searchExp.setEndRow(page.getStartRow() + page.getPageSize());

        // 然后查询并设置当前页数据
        page.setList(getBaseDao().findListToPage(searchExp));
        return page;
    }

    public int findTotalRows(final SearchExpression searchExp)
    {
        return getBaseDao().findTotalRows(searchExp);
    }
}
